package com.nix.web.databases.service.word;

import com.nix.web.databases.mapper.word.MarkWordMapper;
import com.nix.web.databases.pojo.word.MarkWord;
import com.nix.web.databases.pojo.word.Word;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Title: Emotional-Analysis
 * Date: Create in 2018/1/9 1:22
 * Description:
 *
 * @author Jundger
 * @version 1.0
 */
@Service
public class MarkWordService implements MarkWordMapper {
    @Autowired
    MarkWordMapper markWordMapper;

    @Override
    public MarkWord[] getBindWord(Integer id, Integer count) {
        return markWordMapper.getBindWord(id, count);
    }

    @Override
    public MarkWord[] getMarkWord(Integer id, Integer repetitions, Integer count, String category) throws Exception {
        MarkWord[] words = new MarkWord[count];
        MarkWord[] bindWords = markWordMapper.getBindWord(id, count); // 获取该用户已经绑定的单词

        if (bindWords.length > 0) {
            System.arraycopy(bindWords, 0, words, 0, bindWords.length);
        }



        Integer diff = count - bindWords.length;
        System.out.println("bindWords length -->" + bindWords.length);
        if (diff > 0) {
            MarkWord[] markWords = markWordMapper.getMarkWord(id, repetitions, diff, category); // 获取该用户一定数量未绑定的单词
            System.out.println("MarkWord length -->" + markWords.length);

            // 绑定单词
            for (MarkWord word : markWords) {
                markWordMapper.bindWords(word.getId(), id);
            }

            if (markWords.length > 0) {
                System.arraycopy(markWords, 0, words, bindWords.length, markWords.length); // 将已绑定和未绑定的单词合并
            }
        }
        return words;
    }

    @Override
    public int bindWords(Integer word, Integer user) throws Exception {
        return markWordMapper.bindWords(word, user);
    }

    @Override
    public Integer getMarkCountById(Integer id) throws Exception {
        return markWordMapper.getMarkCountById(id);
    }

    @Override
    public Integer getMarkWeiboCountById(Integer id) throws Exception {
        return markWordMapper.getMarkWeiboCountById(id);
    }

    @Override
    public boolean add(MarkWord markWord) throws Exception {
        return markWordMapper.add(markWord);
    }

    @Override
    public boolean delete(Integer id) throws Exception {
        return markWordMapper.delete(id);
    }

    @Override
    public boolean update(MarkWord markWord) throws Exception {
        return markWordMapper.update(markWord);
    }

    @Override
    public MarkWord select(Integer id) throws Exception {
        return markWordMapper.select(id);
    }
}
